package com.shangdiao.sd_server.p_client.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shangdiao.sd_server.constant.Constant;
import com.shangdiao.sd_server.constant.ErrorCode;
import com.shangdiao.sd_server.generator.pojo.SdCompany;
import com.shangdiao.sd_server.generator.pojo.SdInternship;
import com.shangdiao.sd_server.generator.pojo.SdInternshipReview;
import com.shangdiao.sd_server.generator.pojo.SdInternshipReviewExample;
import com.shangdiao.sd_server.generator.pojo.SdPCollection;
import com.shangdiao.sd_server.generator.pojo.SdPCollectionExample;
import com.shangdiao.sd_server.generator.pojo.SdPInternship;
import com.shangdiao.sd_server.generator.pojo.SdPInternshipExample;
import com.shangdiao.sd_server.generator.pojo.SdPParttime;
import com.shangdiao.sd_server.generator.pojo.SdPParttimeExample;
import com.shangdiao.sd_server.generator.pojo.SdParttime;
import com.shangdiao.sd_server.generator.pojo.SdParttimeReview;
import com.shangdiao.sd_server.generator.pojo.SdParttimeReviewExample;
import com.shangdiao.sd_server.pojo.ResponseMsg;
import com.shangdiao.sd_server.pojo.Row;
import com.shangdiao.sd_server.service.CompanyService;
import com.shangdiao.sd_server.service.DicService;
import com.shangdiao.sd_server.service.PersonService;
import com.shangdiao.sd_server.service.PostService;
import com.shangdiao.sd_server.util.DateUtil;
import com.shangdiao.sd_server.util.JsonUtil;
import com.shangdiao.sd_server.util.RequestUtil;
import com.shangdiao.sd_server.util.StringUtil;

/**
 * 个人职位管理
 * @author mary
 *
 */
@Controller
@RequestMapping("/person/oper")
public class PersonOperController {
	
	@Autowired
	PersonService personService;
	
	@Autowired
	PostService postService;
	
	@Autowired
	CompanyService companyService;
	
	@Autowired
	DicService dicService;
	
	
	@RequestMapping(value = { "/updateMyCollection.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg updateMyCollection(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer personId = RequestUtil.getInt(request, "uid");
			Integer postId = RequestUtil.getInt(request, "postId");
			Integer postFlag = RequestUtil.getInt(request, "postFlag");//1实习2兼职
			Integer status = RequestUtil.getInt(request, "status");//1收藏0取消收藏
			if(personId == null || postId == null || postFlag == null || status == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			SdPCollectionExample example = new SdPCollectionExample();
			example.or().andPersonIdEqualTo(personId)
				.andPostIdEqualTo(postId)
				.andPostFlagEqualTo(postFlag)
				.andDelEqualTo(Constant.DEL_NO);
			List<SdPCollection> pCollectionList = personService.selectPCollectionList(example);
			if(pCollectionList != null && pCollectionList.size() != 0){
				SdPCollection pCollection = pCollectionList.get(0);
				pCollection.setStatus(status);
				pCollection.setUpdateTime(new Date());
				personService.updatePCollection(pCollection);
			}else{
				SdPCollection pCollection = new SdPCollection();
				pCollection.setPersonId(personId);
				pCollection.setPostId(postId);
				pCollection.setPostFlag(postFlag);
				pCollection.setStatus(status);
				pCollection.setUpdateTime(new Date());
				personService.insertPCollection(pCollection);
			}
			return new ResponseMsg("操作成功", ErrorCode.OK, null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("操作失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/selectMyCollection.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectMyCollection(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer personId = RequestUtil.getInt(request, "uid");
			if(personId == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			SdPCollectionExample example = new SdPCollectionExample();
			example.or().andPersonIdEqualTo(personId)
				.andStatusEqualTo(Constant.STATUS_OFF)//收藏的
				.andDelEqualTo(Constant.DEL_NO);//未删除的
			List<SdPCollection> pCollectionList = personService.selectPCollectionList(example);
			Row postRow = new Row();
			List<Row> internshipList = new ArrayList<Row>();//实习
			List<Row> parttimeList = new ArrayList<Row>();//兼职
			for(SdPCollection pCollection : pCollectionList){
				Integer postFlag = pCollection.getPostFlag();//1实习2兼职
				if(postFlag.equals(Constant.POST_FLAG_INTERNSHIP)){//1实习
					Integer internshipId = pCollection.getPostId();
					SdInternship internship = postService.selectInternship(internshipId);
					if(internship != null){
						Row result = new Row();
						Integer companyId = internship.getCompanyId();
						SdCompany company = companyService.selectCompanyById(companyId);
						String logo = company.getLogo();
						if(StringUtil.isBlank(logo)){
							result.put("logo", "");
						}else{
							result.put("logo", Constant.DOMAIN + company.getLogo());//
						}
						result.put("pIId", pCollection.getpIId());
						result.put("postFlag", postFlag);
						result.put("companyName", company.getCompanyName());
						result.put("internshipId", internship.getInternshipId());
						result.put("internshipName", internship.getInternshipName());
						result.put("district", internship.getDistrict());
						result.put("salary", internship.getMinSalary() + "K ~ " + internship.getMaxSalary() + "K");
						result.put("createDate", DateUtil.DateToString(internship.getCreateDate(), DateUtil.FORMAT_TWO));
						internshipList.add(result);
					}
				}else if(postFlag.equals(Constant.POST_FLAG_PARTTIME)){//2兼职
					Integer parttimeId = pCollection.getPostId();
					SdParttime parttime = postService.selectParttime(parttimeId);
					if(parttime != null){
						Row result = new Row();
						Integer companyId = parttime.getCompanyId(); 
						SdCompany company = companyService.selectCompanyById(companyId);
						String logo = company.getLogo();
						if(StringUtil.isBlank(logo)){
							result.put("logo", "");
						}else{
							result.put("logo", Constant.DOMAIN + company.getLogo());//
						}
						result.put("pIId", pCollection.getpIId());
						result.put("postFlag", postFlag);
						result.put("parttimeId", parttime.getParttimeId());   //
						result.put("dicParttimeFieldName", parttime.getDicParttimeFieldName());
						result.put("district", parttime.getDistrict());
						String dicSettlementName = parttime.getDicSettlementName();
						result.put("salary", parttime.getSalary());
						result.put("unit", StringUtil.isBlank(parttime.getUnit()) ? "" : parttime.getUnit());
						result.put("hasAllowance", parttime.getHasAllowance());
						result.put("dicSettlementName", dicSettlementName);
						result.put("startDate", DateUtil.DateToString(parttime.getStartData(), DateUtil.LONG_DATE_FORMAT));
						result.put("endDate", DateUtil.DateToString(parttime.getEndData(), DateUtil.LONG_DATE_FORMAT));
						result.put("workDate", DateUtil.DateToString(parttime.getStartData(), DateUtil.SHORT_DATE_FORMAT) + "至" + DateUtil.DateToString(parttime.getEndData(), DateUtil.SHORT_DATE_FORMAT));
						parttimeList.add(result);
					}
				}
			}
			postRow.put("internshipList", internshipList);
			postRow.put("parttimeList", parttimeList);
			return new ResponseMsg("查询成功", ErrorCode.OK, postRow);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("查询失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/deleteMyCollection.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg deleteMyCollection(HttpServletRequest request, HttpServletResponse response) {
		try{
			String pIIdStr = RequestUtil.get(request, "pIIdStr");
			if(StringUtil.isBlank(pIIdStr)){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			String[] pIIdArr = pIIdStr.split(",");
			List<Integer> pIIdList = new ArrayList<Integer>();
			for(String pIId : pIIdArr ){
				pIIdList.add(Integer.parseInt(pIId));
			}
			SdPCollectionExample example = new SdPCollectionExample();
			example.or().andPIIdIn(pIIdList)
				.andDelEqualTo(Constant.DEL_NO);
			SdPCollection pCollection = new SdPCollection();
			pCollection.setDel(Constant.DEL_YES);
			personService.updatePCollectionByExample(pCollection, example);
			return new ResponseMsg("删除成功", ErrorCode.OK, null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("删除失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	@RequestMapping(value = { "/applyMyPost.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg applyMyPost(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer personId = RequestUtil.getInt(request, "uid");
			Integer postId = RequestUtil.getInt(request, "postId");
			Integer postFlag = RequestUtil.getInt(request, "postFlag");//1实习2兼职
			if(personId == null || postId == null || postFlag == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			if(postFlag == Constant.POST_FLAG_INTERNSHIP){//实习
				SdPInternship pInternship = new SdPInternship();
				pInternship.setPersonId(personId);
				pInternship.setInternshipId(postId);
				pInternship.setSource(Constant.SOURCE_PERSON);
				pInternship.setStatus(Constant.INTERNSHIP_STATUS_APPLY);
				pInternship.setCreateTime(new Date());
				personService.insertPInternship(pInternship);
			}else if(postFlag == Constant.POST_FLAG_PARTTIME){//兼职
				SdPParttime pParttime = new SdPParttime();
				pParttime.setPersonId(personId);
				pParttime.setParttimeId(postId);
				pParttime.setSource(Constant.SOURCE_PERSON);
				pParttime.setStatus(Constant.INTERNSHIP_STATUS_APPLY);
				pParttime.setCreateTime(new Date());
				personService.insertPParttime(pParttime);
			}
			return new ResponseMsg("申请成功", ErrorCode.OK, null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("申请失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/selectMyInternship.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectMyInternship(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer personId = RequestUtil.getInt(request, "uid");
			String status = RequestUtil.get(request, "status");
			int curPage = RequestUtil.getInt(request, "curPage", Constant.DEFAULT_CUR_PAGE);
			int pageSize = RequestUtil.getInt(request, "pageSize", Constant.DEFAULT_PAGE_SIZE);
			if(personId == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			List<Row> resultList = new ArrayList<Row>();
			SdPInternshipExample example = new SdPInternshipExample();
			if(StringUtil.isBlank(status)){
				example.or().andPersonIdEqualTo(personId)
				.andSourceEqualTo(Constant.SOURCE_PERSON)
				.andDelPEqualTo(Constant.DEL_NO);
			}else{
				List<Integer> statusList = new ArrayList<Integer>();
				String[] statusArr = status.split(",");
				for(String statusStr : statusArr){
					statusList.add(Integer.parseInt(statusStr));
				}
				example.or().andPersonIdEqualTo(personId)
				.andStatusIn(statusList)
				.andSourceEqualTo(Constant.SOURCE_PERSON)
				.andDelPEqualTo(Constant.DEL_NO);
			}
			Integer start = (curPage - 1) * pageSize;
			example.setOrderByClause("create_time desc limit " + start + "," + pageSize);
			List<SdPInternship> pInternshipList = personService.selectPInternshipList(example);
			for(SdPInternship pInternship : pInternshipList){
				Row result = new Row();
				Integer internshipId = pInternship.getInternshipId();
				SdInternship internship = postService.selectInternship(internshipId);
				if(internship == null){
					result.put("code", ErrorCode.ERROR);
					result.put("desc", "该岗位已删除");
				}else{
					Integer companyId = internship.getCompanyId();
					SdCompany company = companyService.selectCompanyById(companyId);
					result.put("pPid", pInternship.getpIId());
					String logo = company.getLogo();
					if(StringUtil.isBlank(logo)){
						result.put("logo", "");
					}else{
						result.put("logo", Constant.DOMAIN + logo);
					}
					result.put("companyName", company.getCompanyName());
					result.put("internshipId", internship.getInternshipId());
					result.put("internshipName", internship.getInternshipName());
					result.put("district", internship.getDistrict());
					result.put("createDate", DateUtil.DateToString(internship.getCreateDate(), DateUtil.FORMAT_TWO));
					result.put("salary", internship.getMinSalary() + "K ~ " + internship.getMaxSalary() + "K");
					result.put("status", pInternship.getStatus());
					Date interviewTime = pInternship.getInterviewTime();
					if(interviewTime != null){
						result.put("interviewTime", DateUtil.DateToString(interviewTime, DateUtil.FORMAT_TWO));
					}else{
						result.put("interviewTime", "");
					}
				}
				resultList.add(result);
			}
			return new ResponseMsg("查询成功", ErrorCode.OK, resultList);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("查询失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/selectMyParttime.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg selectMyParttime(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer personId = RequestUtil.getInt(request, "uid");
			String status = RequestUtil.get(request, "status");
			int curPage = RequestUtil.getInt(request, "curPage", Constant.DEFAULT_CUR_PAGE);
			int pageSize = RequestUtil.getInt(request, "pageSize", Constant.DEFAULT_PAGE_SIZE);
			if(personId == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			List<Row> resultList = new ArrayList<Row>();
			SdPParttimeExample example = new SdPParttimeExample();
			if(StringUtil.isBlank(status)){
				example.or().andPersonIdEqualTo(personId)
					.andDelPEqualTo(Constant.DEL_NO);
			}else{
				List<Integer> statusList = new ArrayList<Integer>();
				String[] statusArr = status.split(",");
				for(String statusStr : statusArr){
					statusList.add(Integer.parseInt(statusStr));
				}
				example.or().andPersonIdEqualTo(personId)
					.andStatusIn(statusList)
					.andDelPEqualTo(Constant.DEL_NO);
			}
			Integer start = (curPage - 1) * pageSize;
			example.setOrderByClause("create_time desc limit " + start + "," + pageSize);
			List<SdPParttime> pParttimeList = personService.selectPParttimeList(example);
			for(SdPParttime pParttime : pParttimeList){
				Row result = new Row();
				Integer parttimeId = pParttime.getParttimeId();
				SdParttime parttime = postService.selectParttime(parttimeId);
				if(parttime == null){
					result.put("code", ErrorCode.ERROR);
					result.put("desc", "该岗位已删除");
				}else{
					Integer companyId = parttime.getCompanyId();
					SdCompany company = companyService.selectCompanyById(companyId);
					result.put("pPid", pParttime.getpPId());
					String logo = company.getLogo();
					if(StringUtil.isBlank(logo)){
						result.put("logo", "");
					}else{
						result.put("logo", Constant.DOMAIN + logo);
					}
					result.put("companyName", company.getCompanyName());
					result.put("parttimeId", parttime.getParttimeId());
					result.put("dicParttimeFieldName", parttime.getDicParttimeFieldName());
					result.put("district", parttime.getDistrict());
					String dicSettlementName = parttime.getDicSettlementName();
					result.put("salary", parttime.getSalary());
					result.put("unit", StringUtil.isBlank(parttime.getUnit()) ? "" : parttime.getUnit());
					result.put("hasAllowance", parttime.getHasAllowance());
					result.put("dicSettlementName", dicSettlementName);
					result.put("startDate", DateUtil.DateToString(parttime.getStartData(), DateUtil.LONG_DATE_FORMAT));
					result.put("endDate", DateUtil.DateToString(parttime.getEndData(), DateUtil.LONG_DATE_FORMAT));
					result.put("createTime", DateUtil.DateToString(parttime.getCreateTime(), DateUtil.LONG_DATE_FORMAT));
					Date interviewTime = pParttime.getInterviewTime();
					if(interviewTime != null){
						result.put("interviewTime", DateUtil.DateToString(interviewTime, DateUtil.FORMAT_TWO));
					}else{
						result.put("interviewTime", "");
					}
					Date deadline = parttime.getDeadline();
					if(deadline != null && deadline.before(new Date())){
						result.put("status", Constant.INTERNSHIP_STATUS_EXPIRED);
					}else{
						result.put("status", pParttime.getStatus());
					}
					result.put("source", pParttime.getSource());
				}
				resultList.add(result);
			}
			return new ResponseMsg("查询成功", ErrorCode.OK, resultList);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("查询失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/updateMyInternship.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg updateMyInternship(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer pPid = RequestUtil.getInt(request, "pPid");
			Integer status = RequestUtil.getInt(request, "status");
			Integer delP = RequestUtil.getInt(request, "del");
			if(pPid == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			SdPInternship pInternship = new SdPInternship();
			pInternship.setpIId(pPid);
			pInternship.setStatus(status);
			pInternship.setDelP(delP);
			int operCount = personService.updatePInternship(pInternship);
			if(operCount > 0){
				return new ResponseMsg("操作成功", ErrorCode.OK, null);
			}else{
				return new ResponseMsg("简历记录不存在", ErrorCode.ERROR, null);
			}
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("操作失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/updateMyParttime.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg updateMyParttime(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer pPid = RequestUtil.getInt(request, "pPid");
			Integer status = RequestUtil.getInt(request, "status");
			Integer delP = RequestUtil.getInt(request, "del");
			if(pPid == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			SdPParttime pParttime = new SdPParttime();
			pParttime.setpPId(pPid);
			pParttime.setStatus(status);
			pParttime.setDelP(delP);
			int operCount = personService.updatePParttime(pParttime);
			if(operCount > 0){
				return new ResponseMsg("操作成功", ErrorCode.OK, null);
			}else{
				return new ResponseMsg("简历记录不存在", ErrorCode.ERROR, null);
			}
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("操作失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/reviewMyParttime.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg reviewMyParttime(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer personId = RequestUtil.getInt(request, "uid");
			Integer pPid = RequestUtil.getInt(request, "pPid");
			Integer anonymityFlag = RequestUtil.getInt(request, "anonymityFlag");
			Integer parttimeId = RequestUtil.getInt(request, "parttimeId");
			String review = RequestUtil.get(request, "review");
			if(pPid == null || parttimeId == null || StringUtil.isBlank(review)){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			SdParttimeReviewExample example = new SdParttimeReviewExample();
			example.or().andPersionIdEqualTo(personId)
				.andParttimeIdEqualTo(parttimeId)
				.andDelEqualTo(Constant.DEL_NO);
			int count = personService.countParttimeReview(example);
			if(count > 0){
				return new ResponseMsg("已评论", ErrorCode.ERROR, null);
			}
			SdParttimeReview parttimeReview = new SdParttimeReview();
			parttimeReview.setPersionId(personId);
			parttimeReview.setpPId(pPid);
			parttimeReview.setParttimeId(parttimeId);
			parttimeReview.setReview(review);
			parttimeReview.setAnonymityFlag(anonymityFlag);
			parttimeReview.setCreateTime(new Date());
			personService.insertParttimeReview(parttimeReview);
			return new ResponseMsg("评论成功", ErrorCode.OK, null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("评论失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/reviewMyInternship.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg reviewMyInternship(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer personId = RequestUtil.getInt(request, "uid");
			Integer pPid = RequestUtil.getInt(request, "pPid");
			Integer anonymityFlag = RequestUtil.getInt(request, "anonymityFlag");
			Integer internshipId = RequestUtil.getInt(request, "internshipId");
			String review = RequestUtil.get(request, "review");
			if(pPid == null || internshipId == null || StringUtil.isBlank(review)){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			SdInternshipReviewExample example = new SdInternshipReviewExample();
			example.or().andPersionIdEqualTo(personId)
				.andInternshipIdEqualTo(internshipId)
				.andDelEqualTo(Constant.DEL_NO);
			int count = personService.countInternshipReview(example);
			if(count > 0){
				return new ResponseMsg("已评论", ErrorCode.ERROR, null);
			}
			SdInternshipReview internshipReview = new SdInternshipReview();
			internshipReview.setPersionId(personId);
			internshipReview.setpIId(pPid);
			internshipReview.setInternshipId(internshipId);
			internshipReview.setReview(review);
			internshipReview.setAnonymityFlag(anonymityFlag);
			internshipReview.setCreateTime(new Date());
			personService.insertInternshipReview(internshipReview);
			return new ResponseMsg("评论成功", ErrorCode.OK, null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("评论失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	/**
	 * 查看我是否收藏这个岗位 是否已申请这个岗位
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "/getMyFlag.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg getMyFlag(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer personId = RequestUtil.getInt(request, "uid");
			Integer postId = RequestUtil.getInt(request, "postId");
			Integer postFlag = RequestUtil.getInt(request, "postFlag");
			if(personId == null || postId == null || postFlag == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			Row result = new Row();
			if(postFlag.equals(Constant.POST_FLAG_INTERNSHIP)){//实习
				SdPInternshipExample example = new SdPInternshipExample();
				example.or().andPersonIdEqualTo(personId)
					.andInternshipIdEqualTo(postId)
					.andSourceEqualTo(Constant.SOURCE_PERSON)
					.andDelPEqualTo(Constant.DEL_NO);
				List<SdPInternship> pInternshipList = personService.selectPInternshipList(example);
				if(pInternshipList == null || pInternshipList.size() == 0){
					result.put("applyFlag", Constant.DEL_NO);
				}else{
					result.put("applyFlag", Constant.DEL_YES);
				}
			}else if(postFlag.equals(Constant.POST_FLAG_PARTTIME)){//兼职
				SdPParttimeExample example = new SdPParttimeExample();
				example.or().andPersonIdEqualTo(personId)
					.andParttimeIdEqualTo(postId)
					.andSourceEqualTo(Constant.SOURCE_PERSON)
					.andDelPEqualTo(Constant.DEL_NO);
				List<SdPParttime> pParttimeList = personService.selectPParttimeList(example);
				if(pParttimeList == null || pParttimeList.size() == 0){
					result.put("applyFlag", Constant.DEL_NO);
				}else{
					result.put("applyFlag", Constant.DEL_YES);
				}
			}
			SdPCollectionExample example = new SdPCollectionExample();
			example.or().andPersonIdEqualTo(personId)
				.andPostFlagEqualTo(postFlag)
				.andPostIdEqualTo(postId)
				.andStatusEqualTo(Constant.COLLECT_ON)
				.andDelEqualTo(Constant.DEL_NO);
			List<SdPCollection> list = personService.selectPCollectionList(example);
			if(list == null || list.size() == 0){
				result.put("collectFlag", Constant.COLLECT_OFF);
			}else{
				result.put("collectFlag", Constant.COLLECT_ON);
			}
			return new ResponseMsg("查询成功", ErrorCode.OK, result);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("查询失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	@RequestMapping(value = { "/getMyInvitation.json" })
	@ResponseBody
	@Transactional
	public ResponseMsg getMyInvitation(HttpServletRequest request, HttpServletResponse response) {
		try{
			Integer personId = RequestUtil.getInt(request, "uid");
			Integer postFlag = RequestUtil.getInt(request, "postFlag");
			String status = RequestUtil.get(request, "status");
			int curPage = RequestUtil.getInt(request, "curPage", Constant.DEFAULT_CUR_PAGE);
			int pageSize = RequestUtil.getInt(request, "pageSize", Constant.DEFAULT_PAGE_SIZE);
			if(postFlag == null){
				return new ResponseMsg("缺少必要参数", ErrorCode.ERROR, null);
			}
			int start = (curPage - 1) * pageSize;
			List<Row> resultList = new ArrayList<Row>(); 
			if(postFlag ==  Constant.POST_FLAG_INTERNSHIP){
				SdPInternshipExample example = new SdPInternshipExample();
				if(!StringUtil.isBlank(status)){
					String[] statusArr = status.split(",");
					List<Integer> statusList = new ArrayList<Integer>();
					for(String statusStr : statusArr){
						statusList.add(Integer.parseInt(statusStr));
					}
					example.or().andPersonIdEqualTo(personId)
					.andSourceEqualTo(Constant.SOURCE_COMPANY)
					.andDelPEqualTo(Constant.DEL_NO)
					.andStatusIn(statusList);
				}else{
					example.or().andPersonIdEqualTo(personId)
					.andSourceEqualTo(Constant.SOURCE_COMPANY)
					.andDelPEqualTo(Constant.DEL_NO);
				}
				example.setOrderByClause("create_time desc limit " + start + "," + pageSize);
				List<SdPInternship> pInternshipList = personService.selectPInternshipList(example);
				for(SdPInternship pInternship : pInternshipList){
					Row result = new Row();
					Integer internshipId = pInternship.getInternshipId();
					SdInternship internship = postService.selectInternship(internshipId);
					if(internship == null){
						continue;
					}
					Integer companyId = internship.getCompanyId();
					SdCompany company = companyService.selectCompanyById(companyId);
					result.put("pIid", pInternship.getpIId());
					result.put("companyId", companyId);
					result.put("companyName", company.getCompanyName());
					String logo = company.getLogo();
					if(StringUtil.isBlank(logo)){
						result.put("logo", Constant.DOMAIN + logo);
					}else{
						result.put("logo", "");
					}
					result.put("postId", internshipId);
					result.put("postFlag", postFlag);
					result.put("internshipName", internship.getInternshipName());
					result.put("companyDistrictId", company.getDistrictId());
					result.put("companyDistrict", company.getDistrict() == null ? "" : company.getDistrict());
					result.put("companyLocation", company.getLocation() == null ? "" : company.getLocation());
					result.put("district", internship.getDistrict() == null ? "" : internship.getDistrict());
					result.put("location", internship.getLocation() == null ? "" : internship.getLocation());
					result.put("createDate", DateUtil.DateToString(internship.getCreateDate(), DateUtil.SHORT_DATE_FORMAT));
					result.put("status", pInternship.getStatus());
					resultList.add(result);
				}
			} else if(postFlag ==  Constant.POST_FLAG_PARTTIME){
				SdPParttimeExample example = new SdPParttimeExample();
				
				if(!StringUtil.isBlank(status)){
					String[] statusArr = status.split(",");
					List<Integer> statusList = new ArrayList<Integer>();
					for(String statusStr : statusArr){
						statusList.add(Integer.parseInt(statusStr));
					}
					example.or().andPersonIdEqualTo(personId)
					.andSourceEqualTo(Constant.SOURCE_COMPANY)
					.andDelPEqualTo(Constant.DEL_NO)
					.andStatusIn(statusList);
				}else{
					example.or().andPersonIdEqualTo(personId)
					.andSourceEqualTo(Constant.SOURCE_COMPANY)
					.andDelPEqualTo(Constant.DEL_NO);
				}
				example.setOrderByClause("create_time desc limit " + start + "," + pageSize);
				List<SdPParttime> pParttimeList = personService.selectPParttimeList(example);
				for(SdPParttime pParttime : pParttimeList){
					Row result = new Row();
					Integer parttimeId = pParttime.getParttimeId();
					SdParttime parttime = postService.selectParttime(parttimeId);
					Integer companyId = parttime.getCompanyId();
					SdCompany company = companyService.selectCompanyById(companyId);
					result.put("pPid", pParttime.getpPId());
					result.put("companyId", companyId);
					result.put("companyName", company.getCompanyName());
					String logo = company.getLogo();
					if(StringUtil.isBlank(logo)){
						result.put("logo", Constant.DOMAIN + logo);
					}else{
						result.put("logo", "");
					}
					result.put("postId", parttimeId);
					result.put("postFlag", postFlag);
					result.put("dicParttimeFieldName", parttime.getDicParttimeFieldName());
					result.put("companyDistrictId", company.getDistrictId());
					result.put("companyDistrict", company.getDistrict() == null ? "" : company.getDistrict());
					result.put("companyLocation", company.getLocation() == null ? "" : company.getLocation());
					result.put("createTime", DateUtil.DateToString(parttime.getCreateTime(), DateUtil.SHORT_DATE_FORMAT));
					result.put("status", pParttime.getStatus());
					result.put("district", parttime.getDistrict() == null ? "" : parttime.getDistrict());
					result.put("location", parttime.getLocation() == null ? "" : parttime.getLocation());
					result.put("workDate", DateUtil.DateToString(parttime.getStartData(), DateUtil.SHORT_DATE_FORMAT) + "至" + DateUtil.DateToString(parttime.getEndData(), DateUtil.SHORT_DATE_FORMAT));
					resultList.add(result);
				}
			}
			return new ResponseMsg("查询成功", ErrorCode.OK, resultList);
		}catch(Exception e){
			e.printStackTrace();
			return new ResponseMsg("查询失败", ErrorCode.SERVER_ERROR, null);
		}
	}
	
	
		public static void main(String[] args) {
		
	}
	
}
